home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-04 | 8.6 KB | 289 lines | [TEXT/MPS ] |
- ///////////////////////////////////////////////////////////////////////////////
- //
- // StandardServices.cp
- //
- // This VUAid Service locks a name so no other actor can "use" it. This is
- // useful for applications that check serial numbers and can only be
- // tested on one target one at a time. The second parameter is true if you
- // want to lock the name, and false if you want to unlock it. The third
- // parameter is optional. If you supply a string as a key in the
- // 3rd parameter when you lock a name, then you can not unlock it without
- // supplying the key. This is useful for debugging. CAUTION: If you do not
- // supply a key during lock, then any script can unlock the name at any time.
- // You are assuming they will be fair and not unlock things you locked.
- //
- // The basic rules of operation are:
- // 1) The list of names is a global variable that persists until the
- // application is quit and re-launched.
- // 2) A specific name can occur only once in the list.
- // 3) If a name exists, it is locked by definition. At the time it is
- // locked, an object containing it is created and added to the list.
- // 4) If a name does not exist, it is unlocked by definition. When a
- // locked name is unlocked, its object is deleted from the list.
- // 5) Trying to lock a locked name fails. ( DELETED unless a key is used and it
- // matches the key used in the original lock. If no key is used
- // when trying to lock a locked name, it always fails, even if the
- // original lock did not set a key. DELETED)
- // 6) Trying to unlock a name fails if it is not locked already or if the
- // key does not match.
- //
- //
- // Parameters:
- // string: name to lock (up to 63 characters)
- // Boolean: true to lock, false to unlock
- // string: optional key for unlocking (up to 32 characters)
- //
- // Returns:
- // none
- //
- // Revisions:
- // 11/23/92 Jonathan Marsh created (as DebugStrService -- SBR)
- // 06/19/94 Stuart Russell uses new value extraction (see Request.cp)
- // 10/16/94 Stuart Russell change to Pause from DebugStr
- // 10/30/94 Stuart Russell change to Echo from Pause
- //
- ///////////////////////////////////////////////////////////////////////////////
-
- #pragma segment "Services"
-
- #ifndef __StandardServices__
- #include "StandardServices.h"
- #endif
-
- #ifndef __Application__
- #include "Application.h"
- #endif
-
- #ifndef __THREADS__
- #include "Threads.h"
- #endif
-
- #include "StandardServiceErrors.h"
-
- #define kEchoThreadServiceName "EchoThread"
- #define kEchoNoThreadServiceName "EchoNoThread"
- #define kSetSleepTicksServiceName "SetSleepTicks"
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // Global Variables
- //—————————————————————————————————————————————————————————————————————————————————————
- extern Application* gTheApplication;
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // EchoThreadService::EchoThreadService - constructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- EchoThreadService::EchoThreadService():ThreadedService( kEchoThreadServiceName )
- {
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // EchoThreadService::~EchoThreadService - destructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- EchoThreadService::~EchoThreadService()
- {
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // EchoThreadService::ProcessRequest - implements a service for V.U. to utilize
- //—————————————————————————————————————————————————————————————————————————————————————
- OSErr
- EchoThreadService::ProcessRequest( Request* pReq )
- {
- OSErr tErr;
- short argc;
- ScriptValue* pValueToEcho;
- long pTicksToWait, pBeepEveryN, tReps = 0;
- Boolean pBeepAtEnd;
- long ticksWaited;
- long previousTicks;
- Boolean done;
-
- previousTicks = TickCount();
-
- // Should have two parameters
- argc = pReq->GetParamCount();
- if (!(argc == 4)) // We have an argument mismatch here
- {
- pReq->SetErrorCode(errParameterCount);
- pReq->SetErrorMessage(errParameterCountStr);
- return errParameterCount;
- }
-
- // get pValueToEcho
- tErr = pReq->ExtractValueFromNthParam( 1, &pValueToEcho );
- if (tErr)
- return tErr;
-
- // get pTicksToWait
- tErr = pReq->ExtractValueFromNthParam( 2, &pTicksToWait );
- if (tErr)
- return tErr;
-
- // get pBeepAtEnd
- tErr = pReq->ExtractValueFromNthParam( 3, &pBeepAtEnd );
- if (tErr)
- return tErr;
-
- // get pBeepEveryN
- tErr = pReq->ExtractValueFromNthParam( 4, &pBeepEveryN );
- if (tErr)
- return tErr;
-
- if (pBeepAtEnd)
- SysBeep(1);
-
- done = false;
-
- if( pTicksToWait < 0 )
- SetThreadState( kCurrentThreadID, kStoppedThreadState, kNoThreadID );
-
- ticksWaited = TickCount() - previousTicks;
- if ((ticksWaited >= pTicksToWait) || pReq->HasBeenCanceled())
- done = true;
- while (!done)
- {
- ticksWaited = TickCount() - previousTicks;
- if( (ticksWaited >= pTicksToWait) || CheckForCancel( pReq ) )
- done = true;
-
- if( pBeepEveryN )
- {
- tReps++;
- if( !(tReps % pBeepEveryN) )
- SysBeep(1);
- }
- }
-
- pReq->SetReturnValue(pValueToEcho);
-
- if (pBeepAtEnd)
- SysBeep(1);
-
- return noErr;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // EchoNoThreadService::EchoNoThreadService - constructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- EchoNoThreadService::EchoNoThreadService():Service( kEchoNoThreadServiceName )
- {
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // EchoNoThreadService::~EchoNoThreadService - destructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- EchoNoThreadService::~EchoNoThreadService()
- {
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // EchoNoThreadService::ProcessRequest - implements a service for V.U. to utilize
- //—————————————————————————————————————————————————————————————————————————————————————
- OSErr
- EchoNoThreadService::ProcessRequest( Request* pReq )
- {
- OSErr tErr;
- short argc;
- ScriptValue* pValueToEcho;
- long pTicksToWait, pBeepEveryN, tReps = 0;
- Boolean pBeepAtEnd;
- long ticksWaited;
- long previousTicks;
- Boolean done;
-
- previousTicks = TickCount();
-
- // Should have two parameters
- argc = pReq->GetParamCount();
- if (!(argc == 4)) // We have an argument mismatch here
- {
- pReq->SetErrorCode(errParameterCount);
- pReq->SetErrorMessage(errParameterCountStr);
- return errParameterCount;
- }
-
- // get pValueToEcho
- tErr = pReq->ExtractValueFromNthParam( 1, &pValueToEcho );
- if (tErr)
- return tErr;
-
- // get pTicksToWait
- tErr = pReq->ExtractValueFromNthParam( 2, &pTicksToWait );
- if (tErr)
- return tErr;
-
- // get pBeepAtEnd
- tErr = pReq->ExtractValueFromNthParam( 3, &pBeepAtEnd );
- if (tErr)
- return tErr;
-
- // get pBeepEveryN
- tErr = pReq->ExtractValueFromNthParam( 4, &pBeepEveryN );
- if (tErr)
- return tErr;
-
- if (pBeepAtEnd)
- SysBeep(1);
-
- done = false;
-
- ticksWaited = TickCount() - previousTicks;
- if ((ticksWaited >= pTicksToWait) || pReq->HasBeenCanceled())
- done = true;
- while (!done)
- {
- ticksWaited = TickCount() - previousTicks;
- if ((ticksWaited >= pTicksToWait) || CheckForCancel( pReq ))
- done = true;
-
- if( pBeepEveryN )
- {
- tReps++;
- if( !(tReps % pBeepEveryN) )
- SysBeep(1);
- }
- }
-
- pReq->SetReturnValue(pValueToEcho);
-
- if (pBeepAtEnd)
- SysBeep(1);
-
- return noErr;
- }
-
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // SetSleepTicksService::SetSleepTicksService - constructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- SetSleepTicksService::SetSleepTicksService():Service( kSetSleepTicksServiceName )
- {
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // SetSleepTicksService::~SetSleepTicksService - destructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- SetSleepTicksService::~SetSleepTicksService()
- {
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // SetSleepTicksService::ProcessRequest - implements a service for V.U. to utilize
- //—————————————————————————————————————————————————————————————————————————————————————
- OSErr
- SetSleepTicksService::ProcessRequest( Request* pReq )
- {
- OSErr tErr;
- long pSleepTicks;
-
- tErr = pReq->ExtractValueFromNthParam( 1, &pSleepTicks );
- if( tErr )
- {
- return tErr;
- }
-
- pReq->SetReturnValue( gTheApplication->SetSleepTicks( pSleepTicks ) );
- return noErr;
- }
-
-